﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace FitNesseRunner
{
	public static class EnumerableExtensions
	{
		public static IEnumerable<T> Every<T>(this IEnumerable<T> source, int n)
		{
			var i = 0;
			foreach (var element in source)
			{
				if (i % n == 0)
					yield return element;
				i++;
			}
		}

		public static bool IsEmpty<T>(this IEnumerable<T> source)
		{
			return !source.Any();
		}

		public static bool Any(this IEnumerable source)
		{
			return Enumerable.Any(source.Cast<object>());
		}

		public static int Count(this IEnumerable source)
		{
			return Enumerable.Count(source.Cast<object>());
		}

		public static T FirstOr<T>(this IEnumerable<T> source, T defaultValue)
		{
			if (source.IsEmpty())
				return defaultValue;

			return source.First();
		}

		public static T At<T>(this IEnumerable<IEnumerable<T>> source, int x, int y)
		{
			return source.ElementAt(x).ElementAt(y);
		}
	}
}
